﻿using Frame.Core.IServices.Admin;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Frame.Core.Auth
{
    /// <summary>
    /// 权限处理
    /// </summary>
    public class PermissionHandler : IPermissionHandler
    {
        readonly IUserService _userService;
        public PermissionHandler(IUserService userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 权限验证
        /// </summary>
        /// <param name="powerName"></param>
        /// <returns></returns>
        public async Task<bool> CheckPower(string powerName)
        {
            // 如果权限名为空，则放行
            if (String.IsNullOrEmpty(powerName))
            {
                return true;
            }

            // 当前登陆用户的权限列表
            List<string> rolePowerNames = await _userService.GetPermissionsAsync();
            if (rolePowerNames.Contains(powerName))
            {
                return true;
            }

            return false;

        }
    }
}
